﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Clients;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.Contracts;
using System.Net;
using System.Xml.Linq;

using T.E.ERP.Read.API.Applicantions.Command.SaleOrderInfoCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SaleOrderInfoCommandHandler
{
    public class GetSaleOrderDetailCommandHandler : IRequestHandler<GetSaleOrderDetailCommand, APIResult<Viw_SaleOrderDetail>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<CustomerProfiles> customerBiz;
        private readonly IBaseRepository<Material_BasicModel> meterialModelBiz;
        private readonly IBaseRepository<SaleOrderInfo> orderBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetSaleOrderDetailCommandHandler(IMapper mapper, IBaseRepository<CustomerProfiles> customerBiz, IBaseRepository<Material_BasicModel> meterialModelBiz, IBaseRepository<SaleOrderInfo> orderBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.customerBiz = customerBiz;
            this.meterialModelBiz = meterialModelBiz;
            this.orderBiz = orderBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<Viw_SaleOrderDetail>> Handle(GetSaleOrderDetailCommand request, CancellationToken cancellationToken)
        {
            var customers = customerBiz.GetAll().Result;
            var materialModels = meterialModelBiz.GetAll().Result;
            var orderSales = orderBiz.Quary(x => x.Id.ToString() == request.DocumentCode).Result.FirstOrDefault();
            var orderMaterials = orderMaterialsBiz.Quary(x => x.SaleCode.ToString() == request.DocumentCode && x.MaterialsClass == 1).Result.ToList();
            APIResult<Viw_SaleOrderDetail> result = new APIResult<Viw_SaleOrderDetail>();
            result.data = new Viw_SaleOrderDetail
            {
                OrderMaterials = orderMaterials,
                Id = orderSales.Id,
                BusinessType = orderSales.BusinessStatus,
                BusinessStatus = orderSales.BusinessStatus,
                DocumentType = orderSales.BusinessStatus, 
                DocumentStatus = orderSales.BusinessStatus,
                Oprator = orderSales.Oprator,
                CustomerId = orderSales.CustomerId,
                CustomerName = customers.FirstOrDefault(x => x.Uid == orderSales.CustomerId)?.CustomerName,
                DocumentDate = orderSales.DocumentDate,
                Receiver = orderSales.Receiver,
                ExamUser = orderSales.ExamUser,
                Contract = orderSales.Contract,
                Phone = orderSales.Phone,
                SaleDempt = orderSales.SaleDempt,
                ExamDate = orderSales.ExamDate,
                PayDate = orderSales.PayDate,
                Address = orderSales.Address,
                SaleUser = orderSales.SaleUser,
                SaleType = orderSales.SaleType,
                DeliverDate = orderSales.DeliverDate,
                GoalCountry = orderSales.GoalCountry,
                SupplyType = orderSales.SupplyType,
                DeliveryStatus = orderSales.DeliveryStatus,
                Remark = orderSales.Remark,
            };
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = 1;
            return Task.FromResult(result);
        }
    }
}
